﻿using FrameworkCore.Migrations.Data;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;

namespace Domain.Entities.AuditTrails;

[Table("AuditTrails")]
public class AuditTrail : BaseAuditableEntity
{
    public AuditType AuditType { get; set; }
    public string? TableName { get; set; }
    //public string? OldValuesJson 
    //{
    //    get
    //    {
    //        if (OldValues?.Count>0)
    //        {
    //            return JsonSerializer.Serialize(OldValues);
    //        }
    //        return string.Empty;
    //    } 
    //    set { }
    //}
    //public string? NewValuesJson
    //{
    //    get
    //    {
    //        if (NewValues?.Count > 0)
    //        {
    //            return JsonSerializer.Serialize(NewValues);
    //        }
    //        return string.Empty;
    //    }
    //    set { }
    //}
    //public string? AffectedColumnsJson
    //{
    //    get
    //    {
    //        if (AffectedColumns?.Count > 0)
    //        {
    //            return JsonSerializer.Serialize(AffectedColumns);
    //        }
    //        return string.Empty;
    //    }
    //    set { }
    //}
    //public string? PrimaryKeyJson
    //{
    //    get
    //    {
    //        if (PrimaryKey?.Count > 0)
    //        {
    //            return JsonSerializer.Serialize(PrimaryKey);
    //        }
    //        return string.Empty;
    //    }
    //    set { }
    //}


    public Dictionary<string, object?>? OldValues { get; set; }

    public Dictionary<string, object?>? NewValues { get; set; }

    public List<string>? AffectedColumns { get; set; }

    public Dictionary<string, object> PrimaryKey { get; set; } = new();

    public List<PropertyEntry> TemporaryProperties { get; } = new();

    public bool HasTemporaryProperties => TemporaryProperties.Any();
    public string? DebugView { get; set; }
    public string? ErrorMessage { get; set; }
}

public enum AuditType
{
    None,
    Create,
    Update,
    Delete
}